// compile: g++ -o async async.cc -std=c++11
// run: ./async
#include <chrono>
#include <future>
#include <iostream>
#include <thread>
using namespace std;
void Test1() {
  std::future<int> fut2 = std::async(
      std::launch::deferred,  // 同线程，懒惰执行
      [](int a, int b) -> int {
        std::this_thread::sleep_for(std::chrono::seconds(2));
        return a + b;
      },
      3, 5);

  std::future<int> fut1 = std::async(
      std::launch::async,  // 新开线程，立即执行
      [](int a, int b) -> int {
        std::this_thread::sleep_for(std::chrono::seconds(2));
        return a + b;
      },
      3, 5);
  cout << fut1.get() << endl;

  cout << fut2.get() << endl;
}
void Test2() {}
int main() {
  Test1();
  Test2();
  return 0;
}
